package com.example.network_android;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.util.Log;

/**
 * Client is used for network communication
 */

public class Client {
	String host;
	int port;
	
	/**
	 * Der Client Konstruktor nimmt einen String und ein Int...
	 */
	public Client (String h, int p){
		host = h;
		port = p;
		Log.d("Network_android", "created new Client with: "+h+" port: "+p);
	}
	
	public Object send(Object m){
		
		try {
			//Simon	
			// LOOOOL
			Log.d("Network_android", "attemting to send: "+(String)m);
			
			Socket server = new Socket(host,port);
			ObjectOutputStream out = new ObjectOutputStream(server.getOutputStream());
			out.writeObject(m);
			
			ObjectInputStream in = new ObjectInputStream(server.getInputStream());
			
			Object o = in.readObject();
			
			if (o instanceof String){
				return o;
			}
			
			in.close();
			out.close();
			return o;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
}
